package com.shenmazong.lambda;

/**
 * @program: server-java-demo
 * @description: LambdaChange
 * @author: 亮子说编程
 * @create: 2020-10-20 19:59
 **/

//--1 定义一个函数式接口
interface ISayHello {
    public void sayHello();
}

//--2 定义接口实现类
class SayHelloA implements ISayHello {
    @Override
    public void sayHello() {
        System.out.println("say hello, I am A");
    }
}

public class LambdaChange {

    //--4 静态内部类
    static class SayHelloB implements ISayHello {
        @Override
        public void sayHello() {
            System.out.println("say hello, I am B");
        }
    }

    public static void main(String[] args) {

        //--3 使用接口类
        ISayHello hello = new SayHelloA();
        hello.sayHello();

        //--5 使用静态内部类
        hello = new SayHelloB();
        hello.sayHello();

        //--6 局部内部类
        class SayHelloC implements ISayHello {
            @Override
            public void sayHello() {
                System.out.println("say hello, I am C");
            }
        }
        hello = new SayHelloC();
        hello.sayHello();

        //--7 匿名内部类
        hello = new ISayHello() {
            @Override
            public void sayHello() {
                System.out.println("say hello, I am D");
            }
        };
        hello.sayHello();

        //--8 用lambda实现
        hello = () -> {
            System.out.println("say hello, I am E");
        };
        hello.sayHello();

        //--9 简化lambda
        hello = () -> System.out.println("say hello, I am F");
        hello.sayHello();

    }
}
